@extends('layouts._app')
@section('content')
    <body class="iframe-h">
    <div class="layui-tab page-content-wrap">
        <div class="layui-form-item">
            <div class="layui-inline tool-btn">
                <a class="layui-btn layui-btn-small layui-btn-primary" id='email-refresh-btn'
                   href="{{route('expansion.calculation.index')}}" data-title='刷新'>
                    <i class="layui-icon">&#x1002;</i>
                </a>
                <button class="layui-btn layui-btn-small layui-btn-danger delBtn hidden-xs">
                    <i class="layui-icon">&#xe640;</i>
                </button>
            </div>
        </div>
        @include('layouts._flash')
        <ul class="layui-tab-title">
            <li class="layui-this">DNF拍卖行计算器</li>
            <li>普通计算器</li>
        </ul>
        <div class="layui-tab-content">

            <div class="layui-tab-item layui-show">
                {!! Form::open(['class'=>'layui-form col-lg-4','style'=>'margin-top: 30px;float:right;']) !!}
                <div class="layui-form-item">
                    {!! Form::label('buy','* 买入',['class'=>'layui-form-label','style'=>'color:red']) !!}
                    <div class="layui-input-inline" style="width: 60%">
                        <div id="markdown">
                            {!! Form::text('buy',0,['required','lay-verify'=>'required','id'=>'buy','class'=>'layui-input','autocomplete'=>'on']) !!}
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    {!! Form::label('sale','* 卖出',['class'=>'layui-form-label','style'=>'color:red']) !!}
                    <div class="layui-input-inline" style="width: 60%">
                        <div id="markdown">
                            {!! Form::text('sale',0,['required','lay-verify'=>'required','id'=>'sale','class'=>'layui-input']) !!}
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    {!! Form::label('number','* 数量',['class'=>'layui-form-label','style'=>'color:red']) !!}
                    <div class="layui-input-inline" style="width: 60%">
                        <div id="markdown">
                            {!! Form::text('number',0,['required','lay-verify'=>'required','id'=>'number','class'=>'layui-input']) !!}
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    {!! Form::label('results','利润',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-inline" style="width: 60%">
                        <div id="markdown">
                            {!! Form::text('results','',['id'=>'results','class'=>'layui-input','readonly']) !!}
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    {!! Form::label('fee','手续费',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-inline" style="width: 60%">
                        <div id="markdown">
                            {!! Form::text('fee','',['id'=>'fee','class'=>'layui-input','readonly']) !!}
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    {!! Form::label('','拍卖卷：',['class'=>'layui-form-label']) !!}
                    <div class="layui-input-block">
                        <input type="checkbox" name="no" lay-skin="switch" value="yes" lay-text="有|没有" checked>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-inline" style="margin-left: 10%;">
                        <a href="javascript:void(0)" class="layui-btn layui-btn-normal" id="yes">计算</a>
                    </div>
                </div>
                {!! Form::close() !!}
                <div class="layui-form col-lg-8">
                    <table class="layui-table">
                        <thead>
                        <tr>
                            <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                            <th>买入</th>
                            <th>卖出</th>
                            <th>数量</th>
                            <th>利润</th>
                            <th>手续费</th>
                            <th>拍卖卷</th>
                            <th>次数</th>
                            <th>时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($calculation as $value)
                            <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
                            <script src="{{asset('js/common.js')}}" type="text/javascript" charset="utf-8"></script>
                            <tr onclick="navNum({{$value}})">
                                <td><input type="checkbox" name="del" lay-skin="primary" data-id="{{$value->id}}"></td>
                                <td class="nav{{$value->id}}"
                                    title="{{formatNum($value->buy)}}">{{sub(formatNum($value->buy))}}</td>
                                <td class="nav{{$value->id}}"
                                    title="{{formatNum($value->sale)}}">{{sub(formatNum($value->sale))}}</td>
                                <td class="nav{{$value->id}}"
                                    title="{{formatNum($value->number)}}">{{sub(formatNum($value->number))}}</td>
                                <td class="nav{{$value->id}}"
                                    title="{{formatNum($value->results)}}">{{sub(formatNum($value->results))}}</td>
                                <td class="nav{{$value->id}}"
                                    title="{{formatNum($value->fee)}}">{{sub(formatNum($value->fee))}}</td>
                                <td>{{$value->auction == 1 ? '有' : '没有'}}</td>
                                <td class="nav{{$value->id}}"
                                    title="{{formatNum($value->count)}}次">{{sub(formatNum($value->count))}}次
                                </td>
                                <td title="{{$value->updated_at}}">{{sub($value->updated_at,20)}}</td>
                                <td>
                                    <div class="layui-inline">
                                        <a class="layui-btn layui-btn-small layui-btn-danger"
                                           href="{{route('expansion.calculation.destroy',$value->id)}}"
                                           data-method="delete" title="删除"
                                           data-token="{{csrf_token()}}" data-confirm="确认删除吗?">
                                            <i class="layui-icon">&#xe640;</i></a>
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                    <!--tp分页-->
                    <div class="page-wrap">
                        <p style="float: left;">注 : 共 {{$calculation->total()}} 条记录</p>
                        {{ $calculation->links() }}
                    </div>
                </div>
            </div>

            <div class="layui-tab-item">
                <div class="layui-form col-lg-6">
                    <table class="layui-table">
                        <thead>
                        <tr>
                            <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                            <th>公式</th>
                            <th>结果</th>
                            <th>次数</th>
                            <th>时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($pt as $value)
                            <tr>
                                <td><input type="checkbox" name="del" lay-skin="primary" data-id="{{$value->id}}"></td>
                                <td title="{{$value->sale}}">{{sub($value->sale,50)}}</td>
                                <td title="{{$value->results}}">{{sub($value->results,30)}}</td>
                                <td title="{{$value->count}}次">{{sub($value->count)}}次</td>
                                <td title="{{$value->updated_at}}">{{sub($value->updated_at,20)}}</td>
                                <td>
                                    <div class="layui-inline">
                                        <a class="layui-btn layui-btn-small layui-btn-danger"
                                           href="{{route('expansion.calculation.destroy',$value->id)}}"
                                           data-method="delete" title="删除"
                                           data-token="{{csrf_token()}}" data-confirm="确认删除吗?">
                                            <i class="layui-icon">&#xe640;</i></a>
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                    <!--tp分页-->
                    <div class="page-wrap">
                        <p style="float: left;">注 : 共 {{$pt->total()}} 条记录</p>
                        {{ $pt->links() }}
                    </div>
                </div>
                <div class="layui-form col-lg-6">
                    <table class="layui-table" id="table" style="cursor: pointer;text-align: center;">
                        <thead>
                        <tr>
                            <th style="text-align: center;" colspan="4"></th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td title="1">1</td>
                            <td title="2">2</td>
                            <td title="3">3</td>
                            <td title="乘">×</td>
                        </tr>
                        <tr>
                            <td title="4">4</td>
                            <td title="5">5</td>
                            <td title="6">6</td>
                            <td title="减">-</td>
                        </tr>
                        <tr>
                            <td title="7">7</td>
                            <td title="8">8</td>
                            <td title="9">9</td>
                            <td title="加">+</td>
                        </tr>
                        <tr>
                            <td title="删减">删减</td>
                            <td title="0">0</td>
                            <td title="除">÷</td>
                            <td title="结果">=</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <script src="{{asset('static/admin/layui/layui.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('static/admin/js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/destroy.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    </body>
    <script>
        var url = window.location.href;
        url = url.split("?");
        if (url[1]) {
            $("#table th").text(url[1]);
        }
        if (url[2]) {
            var res = {};
            res['buy'] = url[2];
            res['sale'] = url[3];
            res['number'] = url[4];
            res['results'] = url[5];
            res['fee'] = url[6];
            res['auction'] = url[7];
            navNum(res);
        }

        function navNum(res) {
            layui.use(['form'], function () {
                var form = layui.form();
                $("#buy").val(formatNum(res['buy'] + ''));
                $("#sale").val(formatNum(res['sale'] + ''));
                $("#number").val(formatNum(res['number'] + ''));
                $("#results").val(formatNum(res['results'] + ''));
                $("#fee").val(formatNum(res['fee'] + ''));
                if (res['auction'] == 1) {
                    $("input[name='no']").prop('checked', true);
                    form.render();
                } else {
                    $("input[name='no']").prop('checked', false);
                    form.render();
                }
            });
        }

        layui.use(['form', 'jquery', 'element', 'layer', 'dialog'], function () {
            var form = layui.form();
            var layer = layui.layer;
            $ = layui.jquery;
            var dialog = layui.dialog;
            form.render();
            //顶部批量删除
            $('.delBtn').click(function () {
                var ids = [];
                $("input[name='del']:checked").each(function (i) {
                    ids[i] = $(this).data('id');
                });
                if (ids.length == 0) {
                    layer.msg('请选择要删除的', {icon: 5, time: 3000});
                    return false;
                }
                dialog.confirm({
                    message: '您确定要删除选中项',
                    success: function () {
                        $.ajax({
                            type: 'POST',
                            url: '{{route('expansion.calculation.delete')}}',
                            data: {id: ids, filter: true},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
                return false;

            }).mouseenter(function () {

                dialog.tips('批量删除', '.delBtn');

            })
            $("#number").blur(function () {
                $(this).val(formatNum(replaceNum($(this).val())));
            })
            $("#sale").blur(function () {
                $(this).val(formatNum(replaceNum($(this).val())));
            })
            $("#buy").blur(function () {
                $(this).val(formatNum(replaceNum($(this).val())));
            })
            $(document).on('click', '#yes', function () {
                var number = replaceNum($("#number").val());
                var sale = replaceNum($("#sale").val());
                var buy = replaceNum($("#buy").val());
                var no = $("input[name='no']:checked").val();
                var num1 = num(number);
                var num2 = num(sale);
                var num3 = num(buy);
                if (!num1 || !num2 || !num3) {
                    layer.msg('必须填写数字', {icon: 5, time: 3000})
                    return false;
                }
                $.ajax({
                    type: 'POST',
                    url: '{{route('expansion.calculation.max')}}',
                    data: {
                        max: no,
                        number: number,
                        sale: sale,
                        buy: buy,
                    },
                    dataType: "json",
                    beforeSend: function () {
                        layer.load(1, {
                            shade: [0.1, '#333333'] //0.1透明度的白色背景
                        });
                    },
                    complete: function (XHR) {
                        XHR = null;
                    },
                    error: function (xhr) {//获取ajax的错误信息
                        layer.alert(xhr.responseText, "Failed", function () {
                            layer.closeAll();
                        });
                    },
                    success: function (res) {
                        layer.closeAll();
                        if (res.code == 403) {
                            layer.msg(res.msg, {icon: 4, time: 3000})
                            return false;
                        }
                        if (res.code != 200) {
                            layer.msg(res.msg, {icon: 5, time: 3000})
                            return false;
                        }
                        layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                            var max = no == 'yes' ? 1 : 2;
                            window.location.href =
                                "{{route('expansion.calculation.index')}}?" + '?' + buy + '?' + sale + '?' + number + '?' + res.data.results + '' +
                                '?' + res.data.fee + '?' + max;
                        });
                        $("#results").val(formatNum(res.data.results));
                        $("#fee").val(formatNum(res.data.fee));
                    }
                })
            });
        });//DNF计算器

        $("#table td").on('click', function () {
            var text = $(this).text();
            var num = $("#table th").text();
            var nav = false;
            if (text == '删减') {
                $("#table th").text(num.substring(0, num.length - 1));
                return false;
            }
            if (text == '=') {
                nav = passNum(num);
                if (nav) {
                    results = $('#table th').eq(0).text();
                    $.ajax({
                        type: 'POST',
                        url: "{{route('expansion.calculation.store')}}",
                        data: {results: results, status: nav},
                        dataType: "json",
                        beforeSend: function () {
                            layer.load(1, {
                                shade: [0.1, '#333333'] //0.1透明度的白色背景
                            });
                        },
                        complete: function (XHR) {
                            XHR = null;
                        },
                        error: function (xhr) {//获取ajax的错误信息
                            layer.alert(xhr.responseText, "Failed", function () {
                                layer.closeAll();
                            });
                        },
                        success: function (response) {
                            layer.closeAll();
                            if (response.code == 403) {
                                layer.msg(response.msg, {icon: 4, time: 3000});
                                return false;
                            }
                            if (response.code != 200) {
                                layer.msg(response.msg, {icon: 5, time: 3000});
                                return false;
                            }
                            layer.msg(response.msg, {icon: 1, time: 3000}, function () {
                                window.location.href = "{{route('expansion.calculation.index')}}?" + response.data;
                            });
                            $("#table th").text(response.data);
                        }
                    });
                }
            } else {
                if (text == '×' || text == '+' || text == '÷' || text == '-') {
                    nav = passNum(num);
                    if (nav) {
                        var results = $('#table th').eq(0).text();
                        $.ajax({
                            type: 'POST',
                            url: "{{route('expansion.calculation.store')}}",
                            data: {results: results, end: false, status: nav},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (response) {
                                layer.closeAll();
                                if (response.code == 403) {
                                    layer.msg(response.msg, {icon: 4, time: 3000});
                                    return false;
                                }
                                if (response.code != 200) {
                                    layer.msg(response.msg, {icon: 5, time: 3000});
                                    return false;
                                }
                                layer.msg(response.msg, {icon: 1, time: 3000}, function () {
                                    window.location.href = "{{route('expansion.calculation.index')}}?" + response.data;
                                });
                                $("#table th").text(response.data);
                            }
                        });
                    } else {
                        var table = $("#table th").text().replace(/\b(0+)/gi, "");
                        $("#table th").text(table + text);
                    }
                } else {
                    var table = $("#table th").text().replace(/\b(0+)/gi, "");
                    $("#table th").text(table + text);
                }
            }

        });

    </script>
@endsection
